<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增部门' : '编辑部门'" width="1200px" @close='closeDialog'>
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="150px">
	  <el-form-item label="机构编号"  prop="deptNum" style="display:inline-block;width:48%;">
         <el-input v-model="form.deptNum" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="机构名称" prop="name" style="display:inline-block;width:48%;">
        <el-input v-model="form.name" style="width: 370px;"/>
      </el-form-item>
	  <el-form-item v-if="form.pid !== 0" prop="pid"  style="margin-bottom: 0px;display:inline-block;width:48%;" label="上级机构" >
        <treeselect v-model="form.pid" :options="depts" style="width: 370px;" placeholder="选择上级类目"  />
      </el-form-item>
	  <el-form-item v-if="form.pid !== 0"  prop="level"  label="机构级别"  style="display:inline-block;width:48%;margin-top:-20px">
        <el-radio v-for="item in dictmap.dept_level" :key="item.id" v-model="form.level" :label="item.value">{{ item.label }}</el-radio>
      </el-form-item>
      <el-form-item label="人行金融机构代码"  style="display:inline-block;width:48%;">
         <el-input v-model="form.pbcCode" style="width: 370px;"/>
      </el-form-item>
	  <el-form-item label="网点号"  style="display:inline-block;width:48%;">
         <el-input v-model="form.dotNum" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="成立时间" style="display:inline-block;width:48%;">
			<el-form-item prop="date1">
					<el-date-picker  type="date" placeholder="选择日期" v-model="form.foundTime" style="width: 370px;"  value-format="timestamp"></el-date-picker>
			</el-form-item>			
	  </el-form-item>
      <el-form-item label="机构地址"  style="display:inline-block;width:48%;">
         <el-input v-model="form.address" style="width: 370px;"/>
      </el-form-item>
      <el-form-item v-if="form.pid !== 0" label="机构状态" prop="enabled" style="display:inline-block;width:48%;">
        <el-radio v-for="item in dictmap.dept_status" :key="item.id" v-model="form.enabled" :label="item.value">{{ item.label }}</el-radio>
      </el-form-item>

	  <el-form-item label="联系电话"  prop="deptPhone" style="display:inline-block;width:48%;">
         <el-input v-model="form.deptPhone" style="width: 370px;"/>
      </el-form-item>
      <el-form-item  label="备注"  style="display:inline-block;width:93.1%;">
                 <el-input  type="textarea"  v-model="form.remark" ></el-input>
      </el-form-item>

	  <el-form-item label="录入单位"  style="display:inline-block;width:48%;" v-if="form.createDept!==null">
         <el-input  :disabled="true"  v-model="form.createDept.name" style="width: 370px;"/>
      </el-form-item>
	  <el-form-item label="录入日期" style="display:inline-block;width:48%;">
			<el-form-item prop="date1">
					<el-date-picker  type="date" :disabled="true"  v-model="form.createTime" style="width: 370px;" ></el-date-picker>
			</el-form-item>			
	  </el-form-item>

	  <el-form-item label="录入人"  style="display:inline-block;width:48%;" v-if="form.createUser!==null">
         <el-input  :disabled="true"  v-model="form.createUser.username" style="width: 370px;"/>
      </el-form-item>

	  <el-form-item label="更新日期" style="display:inline-block;width:48%;">
			<el-form-item prop="date1">
					<el-date-picker  type="date" :disabled="true"  v-model="form.updateTime" style="width: 370px;"></el-date-picker>
			</el-form-item>			
	  </el-form-item>

	   <el-form-item label="更新人"  style="display:inline-block;width:48%;" v-if="form.updateUser!==null">
         <el-input  :disabled="true"  v-model="form.updateUser.username" style="width: 370px;"/>
      </el-form-item>



	  


    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit, getDepts } from '@/api/dept'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: { Treeselect },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    dicts: {
      type: Array,
      required: true
	},
    dictmap: {
      type: Object,
      required: true
	}
	
  },
  data() {
    return {
      loading: false, dialog: false, depts: [],
      form: {
        id: '',
        name: '',
        pid: 1,
		enabled: 'true',
		level:'',

		deptNum:null,
		pbcCode:null,
		dotNum:null,
		createTime:null,
		address:null,
		deptPhone:null,
		remark:null,
		createDept:null,
		createUser:null,
		updateTime:null,
		updateUser:null,
		foundTime:null
      },
      rules: {
		deptNum:[
           { required: true, message: '请输入机构编号', trigger: 'blur' }
		] , 
        name: [
          { required: true, message: '请输入机构名称', trigger: 'blur' }
		],
		pid: [
          { required: true, message: '请选择上级机构', trigger: 'change' }
		],
		level:[
		  { required: true, message: '请选择机构级别', trigger: 'change' }
		],
		enabled:[
		  { required: true, message: '请选择机构状态', trigger: 'change' }
		],
		deptPhone:[
		  { required: true, message: '请选择机构电话', trigger: 'change' }
		]
      }
    }
  },
  created() {
	  
  },
  methods: {
	closeDialog(){
		this.resetForm()
	},
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.pid !== undefined) {
            this.loading = true
            if (this.isAdd) {
              this.doAdd()
            } else this.doEdit()
          } else {
            this.$message({
              message: '上级部门不能为空',
              type: 'warning'
            })
          }
        }
      })
    },
    doAdd() {
	  this.form.id = '';
	//  this.form.name = '';
	//  this.form.pid = 1;
	  this.form.enabled ='true';

      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
    //   this.$refs['form'].resetFields()
      this.form = {
        id: '',
        name: '',
        pid: 1,
		enabled: 'true',
		level:'',

		deptNum:'',
		pbcCode:'',
		dotNum:'',
		createTime:'',
		address:'',
		deptPhone:'',
		remark:'',
		createDept:'',
		createUser:'',
		updateTime:'',
		updateUser:'',
		foundTime:''
      }
    },
    getDepts() {
      getDepts({ enabled: true }).then(res => {
		this.depts = res.content
		console.log('depts---',this.depts)
      })
    }
  }
}
</script>

<style scoped>
.el-form-item--small.el-form-item{

}

</style>
